package com.example.messageboard.controller;

import com.example.messageboard.model.MessageInfo;
import com.example.messageboard.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;
    @RequestMapping("/publish")
    public Boolean publish(MessageInfo messageInfo){
        log.info("获取参数 messageInfo: {}", messageInfo);
        //参数校验
        if(!StringUtils.hasLength(messageInfo.getFrom()) ||
                !StringUtils.hasLength(messageInfo.getTo())){
            return false;
        }

        //存储数据
        Integer result = messageService.publishMessage(messageInfo);
        if(result > 0){
            return true;
        }
        return false;
    }

    //获取留言信息
    @RequestMapping("/getList")
    public List<MessageInfo> getList(){
        return messageService.getMessageList();
    }
}
